/*+**************************************************************************/
/***                                                                      ***/
/***   This file is distributed under a BSD license.                      ***/
/***   See LICENSE.txt for details.                                       ***/
/***                                                                      ***/
/**************************************************************************+*/

/****************************************************************************/
/***                                                                      ***/
/***   (C) 2005 Dierk Ohlerich, all rights reserved                       ***/
/***                                                                      ***/
/****************************************************************************/

// please ignore the braindead lighting in here --- this is about cubemaps

asc
{
  cbuffer TorusShaderPara : register(c0)
  {
    row_major float4x4 mvp;
    float4x3 mv;
    float4x3 m;
    float4 ld[3];               // light direction
    float4 la;                  // ambient
    float4 lc[4];               // light color

    extern void Set(const sViewport &view,const sMaterialEnv &env)
    {
      sMatrix34 mm;
      sVector30 l[4];

      mvp = view.ModelScreen;
      mv = view.ModelView;
      mm = view.Model;
      mm.Trans3();

      l[0] = env.LightDir[0]*mm;
      l[1] = env.LightDir[1]*mm;
      l[2] = env.LightDir[2]*mm;
      l[3] = env.LightDir[3]*mm;

      ld[0].Init(-l[0].x,-l[1].x,-l[2].x,-l[3].x);
      ld[1].Init(-l[0].y,-l[1].y,-l[2].y,-l[3].y);
      ld[2].Init(-l[0].z,-l[1].z,-l[2].z,-l[3].z);
      la   .InitColor(env.AmbientColor);
      lc[0].InitColor(env.LightColor[0]);
      lc[1].InitColor(env.LightColor[1]);
      lc[2].InitColor(env.LightColor[2]);
      lc[3].InitColor(env.LightColor[3]);

      m = view.Model;
    }
  };
}


material TorusShader
{
  // the vertex shader

  vs
  {    
    asc vs_2_0                 // hlsl code
    {
      use TorusShaderPara;

      void main
      (
        in float3 in_pos : POSITION,            // input, from vertex array
        in float3 in_norm : NORMAL,
        in float2 in_uv : TEXCOORD0,
        out float4 out_col : COLOR0,
        out float3 out_norm : TEXCOORD0,
        out float4 out_pos : POSITION,          // output, to pixel shader
      ) 
      {
        float3 norm = normalize(in_norm);
        float4 i = ld[0]*norm.x + ld[1]*norm.y + ld[2]*norm.z;
        i = max(i,0);
        float4 c = la + lc[0]*i.x + lc[1]*i.y + lc[2]*i.z + lc[3]*i.w;

        out_col = c;
        out_pos = mul(float4(in_pos,1),mvp);
        out_norm = mul(float4(norm,0),m).xyz;
//        out_norm = norm;
      }
    }
  }

  // the pixel shader

  ps
  {

    asc ps_2_0
    {
      samplerCUBE s0 : register(s0);

      void main
      (
        in float4 col : COLOR0,
        in float3 norm : TEXCOORD0,
        out float4 result : COLOR0
      )
      {
        result = col * texCUBE(s0,norm);
      }
    } 
  }
};

/****************************************************************************/

// you may define multiple shaders in one file...

/****************************************************************************/
